package ca.on.eho.qa.util;

import java.io.*;
import java.util.Scanner;


public class TestDataManager {

    public static int getIndex (String searchValue, String [] array)
    {
        for (int i = 0; i < array.length; i++)
        {
            if (array[i].equalsIgnoreCase(searchValue))
            {
                return i;
            }
        }
        return -1;
    }
    
    
    public static String getValue (String userField, String fileName) throws FileNotFoundException, ArrayIndexOutOfBoundsException
    {
        String retVal = null;
        try
        {
            fileName = System.getProperty("user.dir") +"\\"+fileName; //SL
            //File file = new File(fileName);
            BufferedReader br = new BufferedReader( new FileReader(fileName));

            String line = br.readLine();
            String[] cols = line.split(",");
            int index = getIndex(userField, cols);

            if ((line = br.readLine()) != null)
            {
                cols = line.split(",");
                retVal = cols[index];
            }

            br.close();
        }
        catch (IOException ioe)
        {
            System.out.println("IO Exception. " + ioe.toString());
        }
        return retVal;
    }
    
    
    public static void setValue_Bak (String field, String newValue, String fileName) throws FileNotFoundException
    {
        try
        {
            File file = new File(fileName);
            Scanner s = new Scanner(file); //.useDelimiter("\n");


            String newFileName = "Temp" + fileName;
            BufferedWriter nbw = new BufferedWriter(new FileWriter (newFileName));
            File newFile = new File(newFileName);
            Scanner ns = new Scanner(newFile);



            String line = s.next();
            nbw.append(line + "\n");
            String[] cols = line.split(",");
            int index = getIndex(field, cols);
            int index1;
            while (s.hasNext())
            {
                line = s.next();
                cols = line.split(",");
                cols[index] = newValue;
            }

            //release resource
            s.close();

            for (index1 = 0; index1 < cols.length; index1++)
            {
                if (index1 == cols.length - 1)
                {
                    nbw.append(cols[index1]);
                }
                else
                {
                    nbw.append(cols[index1]);
                    nbw.append(",");
                }
            }
            nbw.close();

            BufferedWriter bw = new BufferedWriter(new FileWriter (fileName));
            while(ns.hasNext())
            {
                String tempLine = ns.nextLine();
                String [] tempCols = tempLine.split(",");

                for (int n = 0; n < tempCols.length; n++)
                {
                    if (n == tempCols.length - 1)
                    {
                        bw.append(tempCols[n]);
                        bw.append("\n");
                    }
                    else
                    {
                        bw.append(tempCols[n]);
                        bw.append(",");
                    }
                }
            }
            bw.close();
            ns.close();

            //System.out.println("The new value: " + newValue);

            if (newFile.delete())
            {}
            else
            {
                System.out.println("\nDelete operation unsuccessful");
            }
        }
        catch (IOException io)
        {
            System.out.println("ERROR: File IO Exception" + io.toString());
        }
    }


    public static void setValue (String field, String newValue, String fileName) throws FileNotFoundException
    {
        try
        {
            String curPath=System.getProperty("user.dir") +"\\";  //SL
            fileName = curPath+fileName; //SL
            File file = new File(fileName);
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            Scanner s = new Scanner(file); //.useDelimiter("\n");


     //       String newFileName = "Temp" + fileName;
            String newFileName = fileName.replace(curPath,curPath+"Temp"); //SL
            BufferedWriter nbw = new BufferedWriter(new FileWriter (newFileName));
            File newFile = new File(newFileName);
            Scanner ns = new Scanner(newFile);



            String line = s.next();
            nbw.append(line + "\n");
            String[] cols = line.split(",");
            int index = getIndex(field, cols);
            int index1;
            /**
            while (s.hasNext())
            {
                line = s.next();
                cols = line.split(",");
                cols[index] = newValue;
            }
            */

            while (s.hasNext())
            {
                line = br.readLine();
                if (line == null)
                {
                    break;
                }
                cols = line.split(",");
                cols[index] = newValue;
            }

            //release resource
            br.close();
            s.close();

            for (index1 = 0; index1 < cols.length; index1++)
            {
                if (index1 == cols.length - 1)
                {
                    nbw.append(cols[index1]);
                }
                else
                {
                    nbw.append(cols[index1]);
                    nbw.append(",");
                }
            }
            nbw.close();

            BufferedWriter bw = new BufferedWriter(new FileWriter (fileName));
            while(ns.hasNext())
            {
                String tempLine = ns.nextLine();
                String [] tempCols = tempLine.split(",");

                for (int n = 0; n < tempCols.length; n++)
                {
                    if (n == tempCols.length - 1)
                    {
                        bw.append(tempCols[n]);
                        bw.append("\n");
                    }
                    else
                    {
                        bw.append(tempCols[n]);
                        bw.append(",");
                    }
                }
            }
            bw.close();
            ns.close();

            //System.out.println("The new value: " + newValue);

            if (newFile.delete())
            {}
            else
            {
                System.out.println("\nDelete operation unsuccessful");
            }
        }
        catch (IOException io)
        {
            System.out.println("ERROR: File IO Exception" + io.toString());
        }
    }

    //-----------------------------//

    //Takes CSV file data and puts it in array
    public static String[] indexData1(String fileName) throws IOException {
    	
        String[] cols = null;
        try{
            fileName = System.getProperty("user.dir")+"\\"+"CM_datafiles"+"\\"+fileName; //SL
            //File file = new File(fileName);
            BufferedReader br = new BufferedReader(new FileReader(fileName));

            String line = br.readLine();
            cols = line.split(",");
            br.close();
        }

        catch (IOException ioe)
        {
            System.out.println("IO Exception. " + ioe.toString());
        }
        return cols;
    }
    

    public static String[][] indexData2(String fileName) throws IOException {
        String[][] cols = new String[2][];
        try{
            fileName = System.getProperty("user.dir")+"\\"+"CM_datafiles"+"\\"+fileName; //SL
            //File file = new File(fileName);
            BufferedReader br = new BufferedReader(new FileReader(fileName));

            String line = br.readLine();
            cols[0] = line.split(",");
            if ((line = br.readLine()) != null)
            {
                cols[1] = line.split(",");
            }
            br.close();
        }

        catch (IOException ioe)
        {
            System.out.println("IO Exception. " + ioe.toString());
        }

        return cols;
    }

}